package org.oslog.webapp.web.common.filter;

import java.io.IOException;
import java.util.Map;
import java.util.Set;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.oslog.webapp.web.common.utils.WebUtil;

import com.tomaer.platform.framework.sh.security.core.common.utils.AuditUtil;
import com.tomaer.platform.framework.sh.security.core.common.utils.LogUtil;
import com.tomaer.platform.framework.sh.security.core.sysmain.model.AccessControlEntry;
import com.tomaer.platform.framework.sh.security.core.sysmain.model.User;
import com.tomaer.platform.framework.sh.security.core.sysmain.service.AclService;
import com.tomaer.platform.framework.sh.security.core.sysmain.service.UserService;
import com.tomaer.platform.framework.sh.security.core.sysmain.service.exception.AclServiceException;
import com.tomaer.platform.framework.sh.security.core.sysmain.service.exception.UserServiceException;

public class LoginFilter implements Filter{

	static final LogUtil logger = LogUtil.getLogger(LoginFilter.class);
	
	private UserService userService;
	private AclService aclService;
	
	public void init(FilterConfig arg0) throws ServletException {
		
	}
	
	public void doFilter(ServletRequest arg0, ServletResponse arg1,FilterChain arg2) throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest) arg0;
		String userId = request.getRemoteUser();
		if(StringUtils.isNotEmpty(userId)){
			logger.debug("Logined user id : " + userId +" ,IP address : "+ request.getRemoteAddr());
			if (WebUtil.getUser(request) == null) {
				User user = null;
				try {
					user = userService.getUserById(Long.valueOf(userId));
				} catch (NumberFormatException ex) {
					logger.error("");
					throw new ServletException("",ex);
				} catch (UserServiceException ex) {
					logger.error("");
					throw new ServletException("",ex);
				}
				WebUtil.setUser(request, user);
				Set<String> set = null;
				Map<String,AccessControlEntry> aceMap = null;
				try {
					set = aclService.getUrlSetOfUser(user);
					aceMap = aclService.getACEntriesOfUser(user);
				} catch (AclServiceException ex) {
					logger.error("",ex);
					throw new ServletException("",ex);
				}
				WebUtil.setUrlSet(request, set);
				WebUtil.setACEntries(request, aceMap);
			}
		}
		AuditUtil.setUser(WebUtil.getUser(request));
		arg2.doFilter(arg0, arg1);
		AuditUtil.removeUser();
	}
	
	public void destroy() {
		
	}

	public UserService getUserService() {
		return userService;
	}

	public AclService getAclService() {
		return aclService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public void setAclService(AclService aclService) {
		this.aclService = aclService;
	}
	
}
